package org.dydl.dao.system.repository;

import java.util.Date;
import java.util.List;

import org.dydl.model.system.Productiondetail;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface IProductionDetailRepository extends CrudRepository<Productiondetail, Integer>,JpaSpecificationExecutor<Productiondetail>{

	@Query(value = "FROM Productiondetail ",countQuery = "SELECT count(id) FROM Productiondetail")
	Page<Productiondetail> getProduceList(Pageable pageable);
	
	@Query(value = "SELECT * FROM productiondetail WHERE ALREADY > 0" ,nativeQuery = true)
	List<Productiondetail> getStowageList();
	
	@Query(value = " FROM Productiondetail WHERE orderNo LIKE :orderNo", countQuery = "SELECT count(id) FROM Productiondetail  WHERE orderNo LIKE :orderNo")
	Page<Productiondetail> getProduceListLike(@Param("orderNo") String orderNo, Pageable pageable);
	
	
	@Query(value = "SELECT * FROM productiondetail WHERE ORDER_NO = :orderNo", nativeQuery = true)
	List<Productiondetail> getDetail(@Param("orderNo") String orderNo);
	
	@Query(value = "SELECT ALREADY FROM productiondetail WHERE ORDER_DETAIL_UUID = :orderDetailUuid", nativeQuery = true)
	Integer getAlready(@Param("orderDetailUuid") String orderDetailUuid);

	@Modifying
	@Query(value = "UPDATE productiondetail set ALREADY = ALREADY + :already,REGISTER = :register,REGISTER_DATE = :registerDate WHERE ORDER_DETAIL_UUID = :orderDetailUuid",nativeQuery = true)
	void updateRegister(@Param("already")int already,@Param("register")String register,@Param("registerDate")Date registerDate,@Param("orderDetailUuid")String orderDetailUuid);
	
	@Modifying
	@Query(value = "UPDATE productiondetail set ALREADY_OUT = ALREADY_OUT + :alreadyOut, ALREADY = ALREADY - :alreadyOut WHERE ORDER_DETAIL_UUID = :orderDetailUuid",nativeQuery = true)
	void updateAlreadyOut(@Param("alreadyOut")int alreadyOut,@Param("orderDetailUuid")String orderDetailUuid);
	
	
}